package com.nep.service.impl;

import com.nep.entity.AqiFeedback;
import com.nep.io.FileIO;
import com.nep.service.AqiFeedbackService;

import java.util.ArrayList;
import java.util.List;

public class AqiFeedbackServiceImpl implements AqiFeedbackService {

    @Override
    public void saveFeedBack(AqiFeedback afb) {
        if (afb == null) {
            System.err.println("Error: AqiFeedback object is null");
            return;
        }
        
        List<AqiFeedback> afList = (List<AqiFeedback>)FileIO.readObject("aqifeedback.txt");
        if (afList == null) {
            afList = new ArrayList<>();
        }
        
        afb.setAfId(afList.size() + 1);
        afList.add(afb);
        FileIO.writeObject("aqifeedback.txt", afList);
    }

    @Override
    public void assignGridMember(String afId,String realName) {
        List<AqiFeedback> alist = (List<AqiFeedback>)FileIO.readObject("aqifeedback.txt");
        if (alist == null) {
            System.out.println("错误: 读取aqifeedback.txt失败");
            return;
        }
        
        boolean found = false;
        for (AqiFeedback af : alist) {
            if(af.getAfId().toString().equals(afId)){
                af.setGmName(realName);  // 设置网格员的真实姓名
                af.setState("已指派");
                found = true;
                System.out.println("成功指派网格员: " + realName + " 到反馈ID: " + afId);
                break;
            }
        }
        
        if (!found) {
            System.out.println("警告: 未找到ID为 " + afId + " 的反馈记录");
            return;
        }
        
        FileIO.writeObject("aqifeedback.txt", alist);
        System.out.println("成功保存指派信息到文件");
    }

    @Override
    public void confirmData(AqiFeedback afb) {
        if (afb == null) {
            System.err.println("Error: AqiFeedback object is null");
            return;
        }
        
        List<AqiFeedback> afList = (List<AqiFeedback>)FileIO.readObject("aqifeedback.txt");
        if (afList == null) {
            System.err.println("Error: Failed to read aqifeedback.txt");
            return;
        }
        
        boolean found = false;
        for(int i = 0; i < afList.size(); i++) {
            AqiFeedback a = afList.get(i);
            if(a != null && a.getAfId() != null && a.getAfId().equals(afb.getAfId())) {
                System.out.println("Updating feedback: ID=" + a.getAfId() + 
                                 ", Old State=" + a.getState() + 
                                 ", New State=" + afb.getState());
                a.setState(afb.getState());
                a.setConfirmDate(afb.getConfirmDate());
                a.setCo(afb.getCo());
                a.setSo2(afb.getSo2());
                a.setPm(afb.getPm());
                a.setConfirmLevel(afb.getConfirmLevel());
                a.setConfirmExplain(afb.getConfirmExplain());
                a.setGmName(afb.getGmName());
                found = true;
                break;
            }
        }
        
        if (found) {
            FileIO.writeObject("aqifeedback.txt", afList);
            System.out.println("Successfully updated feedback data");
        } else {
            System.err.println("Error: Could not find AqiFeedback with ID: " + afb.getAfId());
        }
    }
}
